import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_syt/generated/l10n.dart';
import 'package:flutter_syt/starter/i18n/localeization_processor.dart';
import 'package:flutter_syt/utils/click_utils.dart';

/// 加载中
class ViewStateBusyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(child: CircularProgressIndicator());
  }
}

/// 基础Widget
class ViewStateWidget extends StatelessWidget {
  final String? title;
  final String? message;
  final Widget? image;
  final Widget? buttonText;
  final String? buttonTextData;
  final VoidCallback? onPressed;
  final isShowRefreshBtn;
  final Widget defaultImg = Container();

  ViewStateWidget({
    Key? key,
    this.image,
    this.title,
    this.message,
    this.buttonText,
    this.onPressed,
    this.buttonTextData,
    this.isShowRefreshBtn = false,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var titleStyle = TextStyle(fontSize: 14, color: Color(0xff888888));
    // Theme.of(context).textTheme.subtitle1.copyWith(color: Colors.grey);
    var messageStyle = titleStyle.copyWith(
        color: titleStyle.color?.withOpacity(0.7), fontSize: 14);
    return Column(
      // mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: <Widget>[
        SizedBox(
          height: 150.w,
        ),
        image ?? defaultImg,
        // Padding(
        //   padding: const EdgeInsets.symmetric(horizontal: 30, vertical: 30),
        //   child:
        // Column(
        //   mainAxisSize: MainAxisSize.max,
        //   mainAxisAlignment: MainAxisAlignment.start,
        //   children: <Widget>[
        Container(
          height: 12,
        ),
        Text(
          title ?? '',
          style: titleStyle,
        ),
        SizedBox(height: 20),
        ConstrainedBox(
          constraints: BoxConstraints(maxHeight: 200, minHeight: 60),
          child: SingleChildScrollView(
            child: Text(message ?? '', style: messageStyle),
          ),
        ),
        //   ],
        //   // ),
        // ),
        isShowRefreshBtn
            ? Center(
                child: ViewStateButton(
                  child: buttonText ?? Container(),
                  textData: buttonTextData ?? "",
                  // onPressed: ClickUtils.debounce(() {
//                     if (onPressed != null) {
//                       onPressed;
//                     }
// // onPressed ?? () {},
//                   }),
                  onPressed: onPressed ?? () {},
                ),
              )
            : Container(),
      ],
    );
  }
}

/// ErrorWidget
class ViewStateErrorWidget extends StatelessWidget {
  final String? title;
  final String? message;
  final Widget? image;
  final Widget? buttonText;
  final String? buttonTextData;
  final VoidCallback? onPressed;
  final bool isShowRefreshBtn;

  const ViewStateErrorWidget({
    Key? key,
    this.image,
    this.title,
    this.message,
    this.buttonText,
    this.buttonTextData,
    this.onPressed,
    this.isShowRefreshBtn = false,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Image.asset(
            "assets/icons/errorPage.png",
            width: 80,
            height: 80,
          ),
          Container(
            height: 12,
          ),
          Text(
            S.of(context).place_loading_failed,
            style:
                TextStyle(color: Colors.black.withOpacity(0.4), fontSize: 14),
          ),
          Container(
            height: 20,
          ),
          GestureDetector(
            onTap: ClickUtils.debounce(() {
              if (onPressed != null) {
                onPressed!();
              }
            }),
            child: Text(
              S.of(context).place_loading_failed_retry,
              style: TextStyle(color: Color(0xff2942D1), fontSize: 14),
            ),
          )
        ],
      ),
    );
  }
}

/// 页面无数据
class ViewStateEmptyWidget extends StatelessWidget {
  final String? message;
  final Widget? image;
  final Widget? buttonText;
  final VoidCallback? onPressed;
  final bool isShowRefreshBtn;

  const ViewStateEmptyWidget({
    Key? key,
    this.image,
    this.message,
    this.buttonText,
    this.onPressed,
    this.isShowRefreshBtn = false,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ViewStateWidget(
      onPressed: ClickUtils.debounce(() => this.onPressed),
      image: image ??
          Image.asset(
            "assets/icons/empty.png",
            width: 80,
            height: 68,
          ),
      title: message ?? S.of(context).no_content,
      //暂无数据
      buttonText: buttonText,
      buttonTextData: 'Refresh',
      isShowRefreshBtn: isShowRefreshBtn,
    );
  }
}

/// 公用Button
class ViewStateButton extends StatelessWidget {
  final VoidCallback? onPressed;
  final Widget? child;
  final String? textData;

  const ViewStateButton({this.onPressed, this.child, this.textData})
      : assert(child == null || textData == null);

  @override
  Widget build(BuildContext context) {
    if (onPressed == null) {
      return Container();
    }
    return OutlinedButton(
      child: child ??
          Text(
            textData ?? '重试',
            style: TextStyle(wordSpacing: 5),
          ),
      // textColor: Colors.grey,
      // splashColor: Theme.of(context).splashColor,
      onPressed: ClickUtils.debounce(() => onPressed),
      // highlightedBorderColor: Theme.of(context).splashColor,
    );
  }
}
